package com.sosee.cgfw.friends;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sosee.cgfw.R;
import com.sosee.cgfw.utils.Constans;
import com.sosee.cgfw.utils.Utils;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.umeng.analytics.MobclickAgent;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Thinkpad on 2016/3/1.
 * //添加好友 activity
 */
public class FriendsAddNew extends Activity implements View.OnClickListener, AdapterView.OnItemClickListener {
    private ImageButton friends_tel;
    private ImageButton friends_near;
    private ImageButton friends_together;
    private ImageButton friends_back5;
    private  ListView add_newFriends;
    private  RelativeLayout rl_search;
    private HttpUtils httpUtils;
    private FriendsInfoSqData shenqinghaoyou;
    private List<FriendsInfoSqData.Datas> data;
    private MyAddNewFriendsAdapter myAddNewFriendsAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friendsaddnew);
        httpUtils = new HttpUtils();
        //好友申请列表
        requestHttp();
        init();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    private void init() {
        friends_tel=(ImageButton) findViewById(R.id.friends_tel);
        friends_near=(ImageButton) findViewById(R.id.friends_near);
        friends_together=(ImageButton) findViewById(R.id.friends_together);
        friends_back5 = (ImageButton)findViewById(R.id.friends_back5);
        rl_search = (RelativeLayout)findViewById(R.id.rl_search);


        add_newFriends = (ListView)findViewById(R.id.add_newFriends);
        myAddNewFriendsAdapter=new MyAddNewFriendsAdapter();
        //设置显示好友申请。
        add_newFriends.setAdapter(myAddNewFriendsAdapter);
//        add_newFriends.setOnItemClickListener(this);



        friends_tel.setOnClickListener(this);
        friends_near.setOnClickListener(this);
        friends_together.setOnClickListener(this);
        friends_back5.setOnClickListener(this);
        rl_search.setOnClickListener(this);

    }

    private void requestHttp() {

        httpUtils.send(HttpRequest.HttpMethod.GET, Constans.GETAUTID_URL+Constans.userID, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String result = responseInfo.result;
                System.out.println("这是获取审核的信息：" + result);
                Gson gson = new Gson();
                shenqinghaoyou = gson.fromJson(result, FriendsInfoSqData.class);
                data = shenqinghaoyou.getData();
                myAddNewFriendsAdapter.notifyDataSetChanged();



            }

            @Override
            public void onFailure(HttpException e, String s) {


            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.friends_back5:
                finish();
                break;
            //通讯录好友
            case R.id.friends_tel:
                startActivity(new Intent(this, FriendsTel.class));
                break;
            //附近的人
            case R.id.friends_near:
                startActivity(new Intent(this, FriendsNear.class));
                break;
            //共同好友
            case R.id.friends_together:
                startActivity(new Intent(this, FriendsTogether.class));
                break;
            //搜索
            case R.id.rl_search:
                startActivity(new Intent(this, FriendsSearch.class));
                break;
            default:
                break;
        }
    }


    public class MyAddNewFriendsAdapter extends BaseAdapter{
        @Override
        public int getCount() {

            return data==null?0:data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);

        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

                final ViewHolder holder;
                if (convertView == null) {

                    holder = new ViewHolder();
                    convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.friends_friendssq_item, null);



                    holder.friends_pic_sq = (ImageView) convertView.findViewById(R.id.friends_pic_sq);
                    holder.friends_friendsname_sq = (TextView) convertView.findViewById(R.id.friends_friendsname_sq);
                    holder.friends_friendsaniu_sq = (TextView) convertView.findViewById(R.id.friends_friendsaniu_sq);

                    if("0".equals(data.get(position).getFlag())){
                        holder.friends_friendsaniu_sq.setClickable(true);
                    holder.friends_friendsaniu_sq.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                                    httpUtils.send(HttpRequest.HttpMethod.GET, Constans.GETACCPT_URL +Constans.userID+ "&friendsID=" + data.get(position).getData().getID(), new RequestCallBack<String>() {
                                        @Override
                                        public void onSuccess(ResponseInfo<String> responseInfo) {
                                            String s = responseInfo.result;

                                            System.out.println("这是返回的值" + s);
                                            //发送消息到主线程通知更改  加为好友为等待验证。
                                            try {
                                                JSONObject json= new JSONObject(s);
                                                String status = json.getString("status");
                                                if("success".equals(status)){
                                                    holder.friends_friendsaniu_sq.setText("已接受");
                                                }else{

                                                }
                                            } catch (JSONException e) {
                                                e.printStackTrace();
                                            }
//
                                        }

                                        @Override
                                        public void onFailure(HttpException e, String s) {

                                            Utils.showToast("网络获取异常");
                                        }
                            });

                        }
                    });

                    }
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }

                holder.friends_friendsname_sq.setText(data.get(position).getData().getNickname());

                 if("1".equals(data.get(position).getFlag())){

                holder.friends_friendsaniu_sq.setText("已接受");
                }else if("0".equals(data.get(position).getFlag())){

                holder.friends_friendsaniu_sq.setText("接受");

                }else if("-1".equals(data.get(position).getFlag())){

                holder.friends_friendsaniu_sq.setText("已接受");
                 }




                Log.e("hu", convertView.toString());
                return convertView;


        }
        class ViewHolder {
            public ImageView friends_pic_sq;
            public TextView friends_friendsname_sq;
            public TextView friends_friendsaniu_sq;


        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //
        switch (parent.getId()){
            case R.id.add_newFriends:
                //这是
//                Intent intent = new Intent(FriendsAddNew.this,FriendsInfos.class);
//                intent.putExtra("idss", data.get(position).getData().getID());

                //这是
                Intent intent = new Intent(FriendsAddNew.this,FriendsTogetherInfoss.class);
                intent.putExtra("togethers", data.get(position).getData().getID());

                System.out.println("这是传过去的值" + data.get(position).getData().getID());
                startActivity(intent);
                break;
            default:
                break;
        }
    }

}
